using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Terminal : MonoBehaviour
{
    private bool notDone = true;
    private Collider2D collider2D;
    
    private void Awake()
    {
        InitEvent();
        collider2D = GetComponent<Collider2D>();
    }

    private void InitEvent()
    {
        
    }

    private void OnDestroy()
    {
        EasyButton.On_ButtonUp -= OnFuncBtnUp;

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(true);
            EasyButton.On_ButtonUp += OnFuncBtnUp;
        }
#endif
    }

    private void OnTriggerExit2D(Collider2D other)
    {
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(false);
            EasyButton.On_ButtonUp -= OnFuncBtnUp;
        }
#endif
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (Input.GetKey(KeyCode.E) && notDone)
        {
            JieSuan();
            notDone = false;
        }
    }

    private void JieSuan()
    {
        collider2D.enabled = false;
        LevelData levelData = GameController.Instance().archiveCtr.TempLevelData;
        PlayerData playerData = GameController.Instance().playerDataCtr.playerData;
        int addGem = (int) (levelData.goldNum / 2) + (int) (levelData.killed * 10);
        int addKill = levelData.killed;
        playerData.gemNum += addGem;
        playerData.killed += addKill;
        GameController.Instance().playerDataCtr.playerData = playerData;
        GameController.Instance().playerDataCtr.SavePlayerData();
        GameController.Instance().panelCtr.LoadPlayerDataPanel(playerData.gemNum, playerData.killed, addGem, addKill);
    }

    private void OnFuncBtnUp(string btn_name)
    {
        if (!btn_name.Equals("FuncBtn"))
        {
            return;
        }
        JieSuan();
    }
}